package com.lang.service.impl;

import com.lang.dao.*;
import com.lang.dao.impl.*;
import com.lang.entity.Address;
import com.lang.entity.Orders;
import com.lang.entity.User;
import com.lang.service.CartService;
import com.lang.service.OrderService;
import com.lang.service.UserService;
import com.lang.utils.MD5Utils;

import java.util.List;
import java.util.Objects;

/*
 * @Date : 2024/3/12 16:07
 * @Created by Lang
 * @ProjectName : LclShop
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private AddressDao addressDao = new AddressDaoImpl();
    private CartDao cartDao = new CartDaoImpl();
    private OrderDao orderDao = new OrderDaoImpl();

    private ItemDao itemDao = new ItemDaoImpl();


    @Override
    public String getUserByName(String username) {
        User user = userDao.getUserByName(username);
        if (user == null) {
            return "0";
        } else {
            return "1";
        }
    }

    @Override
    public int addUser(User user) {
        return userDao.addUser(user);
    }

    @Override
    public User getUserByCode(String code) {
        return userDao.getUserByCode(code);
    }

    @Override
    public int updateUserStatus(int uId) {
        return userDao.updateStatus(uId);
    }

    @Override
    public User login(String username, String password) {
        User user = userDao.getUserByName(username);
        if (user != null) {
            if (user.getU_password().equals(MD5Utils.md5(password))) {
                return user;
            }
        }
        return null;
    }

    // 不解码（cookies）
    @Override
    public User getUserByCookie(String username, String password) {
        User user = userDao.getUserByName(username);
        if (user != null) {
            if (password.equals(user.getU_password())) {
                return user;
            }
        }
        return null;
    }

    // 管理员登录
    @Override
    public User adminLogin(String username, String password) {
        User user = userDao.adminLogin(username);
        if (user != null) {
            if (user.getU_password().equals(MD5Utils.md5(password))) {
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> list() {
        return userDao.list();
    }

    @Override
    public List<User> search(String username, String gender) {
        if (username == null && gender == null) {
            return userDao.list();
        } else if (username == "") {
            return userDao.searchByGender(gender);
        } else if (gender == "") {
            return userDao.searchByUsername(username);
        } else {
            return userDao.search(username, gender);
        }

    }

    @Override
    public int deleteUser(String id) {
        int res;

        List<Orders> list = orderDao.showOrder(Integer.parseInt(id));
        for (Orders orders : list) {
            String oId = orders.getO_id();
            res =  itemDao.deleteItemByOid(oId);
            System.out.println("删除详情=" + res);
        }

        res = orderDao.deleteOrderByUid(id);
        System.out.println("删除订单=" + res);

        res = cartDao.clearCart(Integer.parseInt(id));
        System.out.println("删除购物车=" + res);

        res = addressDao.deleteAddressByUid(id);
        System.out.println("删除地址=" + res);

        // List<Address> address = addressDao.getAddress(Integer.parseInt(id));
        // for (Address a : address) {
        //     int aId = a.getA_id();
        //     res = addressDao.deleteAddressByAid(String.valueOf(aId));
        //     System.out.println("删除地址=" + res);
        // }
        // List<Orders> list = orderDao.showOrder(Integer.parseInt(id));
        // for (Orders orders : list) {
        //     res = orderDao.deleteOrderByOid(orders.getO_id());
        //     System.out.println("删除订单=" + res);
        // }

        return userDao.deleteUser(id);
    }
}
